package com.mo.lib.utils

import android.annotation.TargetApi
import android.content.res.ColorStateList
import android.graphics.drawable.GradientDrawable
import android.os.Build
import androidx.annotation.IntDef
import androidx.annotation.RequiresApi

/**
 * @ author：mo
 * @ data：2022/4/24:16:39
 * @ 功能：Shape 的构建辅助类
 */

class ShapeBuilder {
    @IntDef(GradientDrawable.RECTANGLE, GradientDrawable.OVAL, GradientDrawable.LINE, GradientDrawable.RING)
    @kotlin.annotation.Retention(AnnotationRetention.SOURCE)
    annotation class Shape

    private val mGradientDrawable=GradientDrawable()
    /** 设置形状*/
    fun setShape(@Shape shape: Int): ShapeBuilder {
        mGradientDrawable.shape = shape
        return this
    }

    /** 设置填充的颜色*/
    fun setColor(fillColor: Int): ShapeBuilder {
        mGradientDrawable.setColor(fillColor)
        return this
    }

  /** 设置填充的颜色*/
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    fun setColor(colorStateList: ColorStateList?): ShapeBuilder {
        mGradientDrawable.color = colorStateList
        return this
    }

    /**设置渐变颜色           colors 至少两种颜色*/
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    fun setColors(vararg colors: Int): ShapeBuilder {
        mGradientDrawable.colors = colors
        return this
    }

    /** 设置描边                strokeWidth 描边宽度                 strokeColor 描边颜色值*/
    fun setStroke(strokeWidth: Int, strokeColor: Int): ShapeBuilder {
        mGradientDrawable.setStroke(strokeWidth, strokeColor)
        return this
    }

    /**设置描边             width  描边宽度       colorStateList 描边颜色选择器   */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    fun setStroke(width: Int, colorStateList: ColorStateList?): ShapeBuilder {
        mGradientDrawable.setStroke(width, colorStateList)
        return this
    }

    /**设置四个角的半径         radius 半径*/
    fun setCornerRadius(radius: Float): ShapeBuilder {
        mGradientDrawable.cornerRadius = radius
        return this
    }

    /** 设置四个角的半径, 排序为: 左上, 右上, 右下, 左下        radius 长度大于等于 8 的数组, 包含了四个角的 X 和 Y 半径   */
    fun setCornerRadii(radius: FloatArray?): ShapeBuilder {
        mGradientDrawable.cornerRadii = radius
        return this
    }

    /** 获取 shape, 以 GradientDrawable 的形式返回*/
    fun getShape(): GradientDrawable {
        return mGradientDrawable
    }


}